//
//  ViewController.swift
//  TestSnapKit
//
//  Created by runone02 on 2019/1/21.
//  Copyright © 2019年 liqiyun. All rights reserved.
//

import UIKit
import SnapKit

class ViewController: UIViewController {
  var viewTow : UIView!
  var loadBtn : UIButton?
  
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "snapKit"
      var arr = ["1","2","3","4"]
      arr[3] = "6"
      print(arr)
    self.view.addSubview(phoneTF)
    self.view.addSubview(passwordTF)
    phoneTF.delegate = self
    passwordTF.delegate = self
    }
    

    ///手机号码
    let phoneTF : loadTextField = {
        let textField = loadTextField(frame: CGRect(x: 24, y: 300, width: ScreenInfo.Width - 48, height: 35), isSecure: false)
        //        textField.label.text = "手机号"
        textField.placeholder = "手机号"
        textField.font = UIFont.systemFont(ofSize: 18)
        textField.clearButtonMode = .always
        textField.maxTextNumber = 11
        textField.keyboardType = .phonePad
        textField.changeLineHidden()///设置底部线
        return textField
    }()
    ///密码
    let passwordTF : loadTextField = {
        let textField = loadTextField(frame: CGRect(x: 24, y: 350, width: ScreenInfo.Width - 48, height: 35), isSecure: true)
        //        textField.label.text = "密码"
        textField.placeholder = "密码"
        textField.maxTextNumber = 15
        textField.font = UIFont.systemFont(ofSize: 18)
        textField.changeLineHidden()///设置底部线
        return textField
    }()
    
    
    override func loadView() {
        super.loadView()
        let bgView = UIView()
        bgView.backgroundColor = UIColor.white
        view.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalTo(view)
        }
        
        let viewOne = UIView()
        viewOne.backgroundColor = UIColor.red
        let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(iconClick))
        viewOne.addGestureRecognizer(singleTapGesture)
        viewOne.isUserInteractionEnabled = true
        view.addSubview(viewOne)
        viewOne.snp.makeConstraints { (make) in
            make.top.equalTo(64)
            make.left.equalTo(10)
            make.width.equalTo(30)
            make.height.equalTo(30)
        }

        viewTow = UIView()
        viewTow.backgroundColor = UIColor.black
        view.addSubview(viewTow)
        viewTow.snp.makeConstraints { (make) in
            make.top.equalTo(viewOne)
            make.left.equalTo(viewOne.snp.right).offset(10)
            make.width.equalTo(80)
            make.height.equalTo(50)
        }
        
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.text = "测试使用snapkit"
        label.textColor = UIColor.black
        view.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.centerY.equalTo(viewTow)
            make.left.equalTo(viewTow.snp.right).offset(10)
        }
        
        loadBtn = UIButton(type: .custom)
        loadBtn?.backgroundColor = UIColor.red
        loadBtn?.setTitle("按钮", for: .normal)
        loadBtn?.setTitle("点击", for: .selected)
        
        loadBtn?.addTarget(self, action: #selector(loadButtonClick(button:)), for: .touchUpInside)
        view.addSubview(loadBtn!)
        loadBtn?.snp.makeConstraints({ (make) in
            make.top.equalTo(label.snp.bottom).offset(10)
            make.left.equalTo(label)
        })
       loadbackButton()
    }
    
    @objc fileprivate func iconClick(){
        
        let TF = TFViewController()
        self.navigationController?.pushViewController(TF, animated: true)
        
    }
    
    
    @objc fileprivate func loadButtonClick(button:UIButton){
    button.isSelected = !button.isSelected
        if button.isSelected {
            viewTow.backgroundColor = UIColor.orange
            viewTow.snp.updateConstraints { (make) in
                make.height.equalTo(100)
                make.width.equalTo(100)
            }
        }else{
            viewTow.backgroundColor = UIColor.black
            viewTow.snp.updateConstraints { (make) in
                make.height.equalTo(80)
                make.width.equalTo(50)
            }
        }
    }
    

    // 懒加载按钮
    lazy var backButton:UIButton = {
        let backButton = UIButton()
        backButton.backgroundColor = UIColor.red
        backButton.setTitle("跳转", for:.normal)
        backButton.setTitle("选择", for:.selected)
        backButton.addTarget(self, action: #selector(btnAction(button:)), for:.touchUpInside)
        return backButton
    }()
    
    
    lazy var backView : UIView = {
        let backView = UIView()
        backView.backgroundColor = UIColor.black
        backView.layer.cornerRadius = 30
        backView.layer.masksToBounds = true
        backView.layer.borderWidth = 2
        backView.layer.borderColor = UIColor.red.cgColor
        return backView
    }()
    
    lazy var FT : UITextField = {
       let FT = UITextField()
        FT.placeholder = "输入法"
        FT.font = UIFont.systemFont(ofSize: 13)
        FT.layer.borderColor = UIColor.red.cgColor
        FT.layer.borderWidth = 1
        ///光标位置向右移
//        FT.setValue(NSNumber(value: 12), forKey: "paddingLeft")
        
        FT.clearButtonMode = .whileEditing  //编辑时出现清除按钮
//        FT.clearButtonMode = .unlessEditing  //编辑时不出现，编辑后才出现清除按钮
//        FT.clearButtonMode = .always  //一直显示清除按钮
        FT.isSecureTextEntry = true //输入内容会显示成小黑点
        FT.becomeFirstResponder() //使文本框在界面打开时就获取焦点，并弹出输入键盘
        FT.resignFirstResponder()  //使文本框失去焦点，并收回键盘
        FT.adjustsFontSizeToFitWidth = true  //当文字超出文本框宽度时，自动调整文字大小
        FT.leftView = UIImageView(image: UIImage(named: "accidentLive"))///设置左边的图片
        FT.leftViewMode = UITextField.ViewMode.always

        return FT
    }()
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//         FT.resignFirstResponder()
        /// 点击任意位置取消第一响应,弹回键盘
         self.view.endEditing(true)
    }
    
    ///添加按钮
   private func loadbackButton(){
      view.addSubview(backButton)
      backButton.snp.makeConstraints { (make) in
        make.top.equalTo((loadBtn?.snp.bottom)!).offset(20)
            make.left.equalTo(100)
            make.width.equalTo(60)
            make.height.equalTo(60)
        }
    
       view.addSubview(backView)
       backView.snp.makeConstraints { (make) in
            make.top.equalTo(backButton.snp.bottom).offset(20)
            make.left.equalTo(backButton)
            make.width.height.equalTo(backButton)
        }
    
       view.addSubview(FT)
       FT.snp.makeConstraints { (make) in
            make.top.equalTo(backButton)
            make.left.equalTo(backButton.snp.right).offset(10)
            make.width.equalTo(80)
            make.height.equalTo(30)
        }
    }
    

    
   /// 按钮点击
   @objc func btnAction(button:UIButton){
        button.isSelected = !button.isSelected
    if button.isSelected{
        
        backButton.snp.updateConstraints { (make) in
            make.top.equalTo((loadBtn?.snp.bottom)!).offset(20)
            make.left.equalTo(100)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
       
    }else{
        backButton.snp.updateConstraints { (make) in
            make.top.equalTo((loadBtn?.snp.bottom)!).offset(20)
            make.left.equalTo(100)
            make.width.equalTo(60)
            make.height.equalTo(60)
        }
    }
   
    }
}

extension ViewController: UITextFieldDelegate{
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if textField is loadTextField {
            let tf = textField as! loadTextField
            if tf.text == ""{
//                tf.changeLabel()
            }
//            tf.changeLineHidden()
        }
        return true
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        if textField is loadTextField {
            let tf = textField as! loadTextField
            if tf.text == "" {
//                tf.disChangeLabel()
            }
//            tf.changeLineHidden()
        }
    }
}

